package com.web.customer.service;

import com.web.account.service.AccountService;
import com.web.common.BaseResult;
import com.web.common.PendingAmend;
import com.web.common.enums.BaseStatus;
import com.web.customer.entity.Customer;
import com.web.customer.mapper.CustomerMapper;
import com.web.pendingList.entity.PendingList;
import com.web.pendingList.service.PendingListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wenzhuo
 * @create 2024-01-09 2024/1/9
 */
@Service
public class CustomerService implements PendingAmend {

    @Resource
    private  CustomerMapper customerMapper;

    @Resource
    private PendingListService pendingListService;

    @Resource
    private AccountService accountService;

    public List<Customer> getCustomerList(Integer customerNo,String customerGroup,String customerName){
       return customerMapper.getCustomers(customerNo,customerGroup,customerName);
    }

    public Customer QueryDuplicateData(String customerGroup,String customerName){
        return customerMapper.getDuplicateData(customerGroup,customerName);
    }

    public Integer addCustomer(Customer customer){
        return customerMapper.addCustomer(customer);
    }

    public Customer checkCustomer(Integer pendingNumber){
        return customerMapper.getCustomerByPendingNumber(pendingNumber);
    }

    public Integer updateCustomer(Customer customer){
       return customerMapper.amendCustomer(customer);
    };

    public Integer deleteCustomer(String customerNo){
        return customerMapper.logicalDeleteCustomer(customerNo);
    };

    public Integer updatePendingNumber(Customer customer){
        return customerMapper.updatePendingNumber(customer);
    };

    public Integer addTemporaryData(Customer customerNew) {
        return customerMapper.addTemporaryCustomer(customerNew);
    }

    @Override
    @Transactional
    public BaseResult rejectAddPending(PendingList pendingTxn) {

        //查询Customer数据是否存在
        Customer customer = checkCustomer(pendingTxn.getPendingNum());
        if(customer == null){
            return new BaseResult(BaseStatus.CUSTOMER_NOT_EXIST.getCode(),BaseStatus.CUSTOMER_NOT_EXIST.getDesc());
        }

        //如果存在就将Customer数据进行物理删除
        if(customerMapper.physicsDeleteCustomer(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //修改审批数据的状态
        if(pendingListService.updatePendingTxn(PendingList.makeReject(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult approveAddPending(PendingList pendingTxn) {
        //根据PendingNumber查询出Customer
        Customer customer = customerMapper.getCustomerByPendingNumber(pendingTxn.getPendingNum());


        //将Customer的APPROVED栏位标记为 Y,代表审批通过,同时将PendingNumebr设置为null
        if(customerMapper.updateApprove(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //将Pending表中的status修改为APP,代表审批通过
        if(pendingListService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) < 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        }

        //为新增的customer新增对应的account(账户)信息
        if(accountService.generateAccountByCustomer(customer) <= 0){
            return new BaseResult(BaseStatus.INSERT_ACCOUNT_FAILED.getCode(),BaseStatus.INSERT_ACCOUNT_FAILED.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult rejectDeletePending(PendingList pendingTxn) {
        //將Pending记录状态修改为REJ.
        if(pendingListService.updatePendingTxn(PendingList.makeReject(pendingTxn)) <= 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        };

        //将Customer交易的PendingNumber设置为null
        if(customerMapper.updateReject(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult approveDeletePending(PendingList pendingTxn) {
        //根据PendingNumber查询出Customer
        Customer customer = customerMapper.getCustomerByPendingNumber(pendingTxn.getPendingNum());


        //將Pending记录状态修改为APP.
        if(pendingListService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) <= 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        };

        //物理删除Customer记录
        if(customerMapper.physicsDeleteCustomer(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //物理删除Customer的Account信息
        if(accountService.physicsDeleteAccount(customer.getCustomerNo()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult rejectModifyPending(PendingList pendingTxn) {
        //根据PendingNumber查询出Customer
        Customer customer = customerMapper.getCustomerByPendingNumber(pendingTxn.getPendingNum());

        //将审批数据的状态改为REJ.
        if(pendingListService.updatePendingTxn(PendingList.makeReject(pendingTxn)) <= 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        };

        //将Customer交易的PendingNumber设置为null
        if(customerMapper.updateReject(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        //将临时表中的数据进行物理删除
        if(customerMapper.physicsDeleteTemporaryCustomer(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_REJECT_SUCCESS.getDesc());
    }

    @Override
    @Transactional
    public BaseResult approveModifyPending(PendingList pendingTxn) {
        //根据PendingNumber查询出CustomerOld
        Customer customerOld = customerMapper.getCustomerByPendingNumber(pendingTxn.getPendingNum());

        //根据PendingNumber查询出CustomerNew
        Customer customerNew = customerMapper.getTemporaryCustomer(pendingTxn.getPendingNum());

        //将PendingTxn状态改为APP.
        if(pendingListService.updatePendingTxn(PendingList.makeApprove(pendingTxn)) <= 0){
            return new BaseResult(BaseStatus.PENDING__DATA_EXCEPTION.getCode(),BaseStatus.PENDING__DATA_EXCEPTION.getDesc());
        };

        //修改customerOld的数据
        customerOld.setCustomerGroup(customerNew.getCustomerGroup());
        if(customerMapper.updateSourceCustomer(customerOld) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        };

        //将临时表中的数据进行物理删除
        if(customerMapper.physicsDeleteTemporaryCustomer(pendingTxn.getPendingNum()) <= 0){
            return new BaseResult(BaseStatus.CUSTOMER__DATA_EXCEPTION.getCode(),BaseStatus.CUSTOMER__DATA_EXCEPTION.getDesc());
        }


        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.PENDING_APPROVE_SUCCESS.getDesc());
    }

}
